package com.example.wanandroid.ui.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.wanandroid.R;
import com.example.wanandroid.core.bean.BaseData;
import com.example.wanandroid.core.bean.BaseResponse;
import com.example.wanandroid.core.bean.main.banner.BannerData;
import com.example.wanandroid.core.bean.main.banner.BannerListData;
import com.example.wanandroid.core.bean.main.collect.FeedArticleListData;
import com.example.wanandroid.core.http.api.Apis;
import com.example.wanandroid.core.utils.HttpUtil;
import com.example.wanandroid.ui.adapter.ArticleListAdapter;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @author jiangyi
 */
public class ArticleListFragment extends BaseFragment{
    private String TAG = "ArticleListFragment";
    View view;
    RecyclerView recyclerView;
    RefreshLayout refreshLayout;
    LinearLayoutManager layoutManager;
    ArticleListAdapter articleListAdapter;
    Retrofit retrofit;
    Apis apis;
    volatile int page = 1;

    @Override
    protected void showLoading() {

    }
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.article_list_fragment, container, false);

        initFragment();
        getBannerData();
        getFirstPageData();
        return view;
    }

    private void initFragment(){
        //初始化RecyclerView
        recyclerView = view.findViewById(R.id.article_list);
        layoutManager = new LinearLayoutManager(getActivity());
        articleListAdapter = new ArticleListAdapter(getActivity());
        recyclerView.setAdapter(articleListAdapter);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setOnScrollChangeListener((v,scrollX,scrollY,oldX,oldY)->{
            int firstVisiblePosition = layoutManager.findFirstVisibleItemPosition();
            Log.d(TAG, "initFragment: "+firstVisiblePosition);
        });

        //初始化网络请求
        apis = HttpUtil.getApi();

        //初始化刷新加载框架
        refreshLayout = view.findViewById(R.id.refresh_layout);
        refreshLayout.setRefreshHeader(new ClassicsHeader(getContext()));
        refreshLayout.setRefreshFooter(new ClassicsFooter(getContext()));
        refreshLayout.setOnRefreshListener(refreshlayout -> {
            getFirstPageData();
            refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
        });
        refreshLayout.setOnLoadMoreListener(refreshlayout -> {
            updateData();
            refreshlayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败
        });
    }

    private void getBannerData(){
        Call<BaseResponse<List<BannerData>>> bannerDataCall = apis.getBannerData();
        bannerDataCall.enqueue(new Callback<BaseResponse<List<BannerData>>>() {
            @Override
            public void onResponse(Call<BaseResponse<List<BannerData>>> call, Response<BaseResponse<List<BannerData>>> response) {
                BannerListData bannerListData = new BannerListData();
                bannerListData.setBannerDataList(response.body().getData());
                articleListAdapter.addData(bannerListData);
            }

            @Override
            public void onFailure(Call<BaseResponse<List<BannerData>>> call, Throwable t) {

            }
        });
    }

    private void getFirstPageData(){

        Call<BaseResponse<FeedArticleListData>> articleDataCall = apis.getFeedArticleList(0);
        articleDataCall.enqueue(new Callback<BaseResponse<FeedArticleListData>>() {
            @Override
            public void onResponse(Call<BaseResponse<FeedArticleListData>> call, Response<BaseResponse<FeedArticleListData>> response) {
                FeedArticleListData feedArticleListData = response.body().getData();
                List<BaseData> feedArticleDataList = new ArrayList<>(feedArticleListData.getDatas());
                articleListAdapter.addDataList(feedArticleDataList);
            }

            @Override
            public void onFailure(Call<BaseResponse<FeedArticleListData>> call, Throwable t) {
                t.printStackTrace();
            }
        });

    }

    private void updateData(){
        Call<BaseResponse<FeedArticleListData>> articleDataCall = apis.getFeedArticleList(page);
        articleDataCall.enqueue(new Callback<BaseResponse<FeedArticleListData>>() {
            @Override
            public void onResponse(Call<BaseResponse<FeedArticleListData>> call, Response<BaseResponse<FeedArticleListData>> response) {
                FeedArticleListData feedArticleListData = response.body().getData();
                List<BaseData> feedArticleDataList = new ArrayList<>(feedArticleListData.getDatas());
                articleListAdapter.addDataList(feedArticleDataList);
                page ++;
            }

            @Override
            public void onFailure(Call<BaseResponse<FeedArticleListData>> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        recyclerView = null;
        layoutManager = null;
        articleListAdapter = null;
    }
}
